package com.lei.emos.wx.aop;


import com.lei.emos.wx.common.util.R;
import com.lei.emos.wx.config.shiro.ThreadLocalToken;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 令牌更新
 * 把更新后的令牌写到ThreadLocalToken里面的ThreadLocal
 */
@Aspect
@Component
public class TokenAspect {

    @Autowired
    private ThreadLocalToken threadLocalToken;

    /**
     * 切入点
     */
    @Pointcut("execution(public * com.lei.emos.wx.controller.*.*(..)))")
    public void aspect(){}

    /**
     * aop事件
     * 拦截所有Web方法的返回值，在返回的R对象中添加更新后的令牌
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("aspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        R r = (R)point.proceed();
        String token = threadLocalToken.getToken();
        if (token!=null){
            r.put("token",token);
            threadLocalToken.clear();
        }
        return r;
    }
}
